﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;

namespace School.Pages.Evaluation
{
    public partial class Behaviour_Evaluation_types : System.Web.UI.Page
    {
        public static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gv_eva_type();

                Label1.Text = "";
                TextBox1.Text = "";
            }
            gv_eva_type();
        }

        private void gv_eva_type()
        {
            var q1 = from r in db.TE_Evaluation_Type
                     select r;
            GridView1.DataSource = q1;
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text != "")
            {
                TE_Evaluation_Type type = new TE_Evaluation_Type() { Name = TextBox1.Text };
                db.AddToTE_Evaluation_Type(type);
                db.SaveChanges();
                gv_eva_type();
            }
            else
            {
                gv_eva_type();
                Label1.Text = "ادخل الاسم بطريقه سليمه ";

            }

        }
        
        
        
        protected void gridview1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            gv_eva_type();
        }
        
        
        
        protected void gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            gv_eva_type();
        }


        
        
        protected void gridview_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int id = int.Parse(((Label)(GridView1.Rows[e.RowIndex].FindControl("l_id"))).Text);
                var q1 = (from r in db.TE_Evaluation_Type
                          where r.Id == id
                          select r).First();
                db.DeleteObject(q1);
                db.SaveChanges();
                gv_eva_type();
            }
            catch
            {
                gv_eva_type();
                Label1.Text = "لايمكن حذف جميع التقيمات";
            }
        }



        
        
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           

            string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("t_name")).Text;
            
            if (name != "")
            {
                int id = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("t_id"))).Text);


                var q1 = (from r in db.TE_Evaluation_Type
                         where r.Id == id
                         select r).First();
               

                q1.Name = name;
                db.SaveChanges();
                GridView1.EditIndex = -1;
                gv_eva_type();
            }
            else
            {
                gv_eva_type();
                Label1.Text = "تاكد من ادخال نوع التقيم بطريقه صحيحه";
            }

        }
    }
}